//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using OpenLS.UI.ViewModels;

namespace OpenLS.UI
{
    /// <summary>
    /// Interaction logic for DialogWindow.xaml
    /// </summary>
    public partial class DialogWindow
    {
        private DialogWindowViewModel _dialogWindowViewModel;
        
        public DialogWindow()
        {
            InitializeComponent();
            _dialogWindowViewModel = new DialogWindowViewModel();
            this.DataContext = _dialogWindowViewModel;
        }

        private Func<bool> ValidateDialogWindow;

        public void SetValidator<T>(Func<T, bool> HasError) where T : FrameworkElement
        {
            if (Content is T)
                ValidateDialogWindow = () => (HasError((T)Content));
        }

        void onButtonClick(object sender, RoutedEventArgs args)
        {

            Button button = args.Source as Button;
            if (button == null)
                button = args.OriginalSource as Button;
            if (button == null)
                return;
            
            if (button.IsCancel)
            {
                DialogResult = false;
                Close();
                return;
            }

            if (ValidateDialogWindow != null)
            {
                if (!ValidateDialogWindow())
                {
                    _dialogWindowViewModel.ShowsFront = false;
                    return;
                }
               
            }
            _dialogWindowViewModel.ShowsFront = true;
             
                if (button.IsDefault)
                {
                    DialogResult = true;
                    Close();
                    return;
                }
               
        }

        internal bool isDialog;

        private void onKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                if(isDialog)
                {
                    DialogResult = false;                    
                }
                Close();
            }

        }
        
    }
}
